﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MPosters.Models;
using System.Web.Helpers;
using System.IO;

namespace MPosters.Controllers
{ 
    public class PosterManagerController : Controller
    {
        private PosterStoreEntities db = new PosterStoreEntities();

        //
        // GET: /PosterManager/

        public ViewResult Index()
        {
            var posters = db.Posters.Include(a => a.Board);
            return View(posters.ToList());
        }

        //
        // GET: /PosterManager/Details/5

        public ViewResult Details(int id)
        {
            Poster poster = db.Posters.Find(id);
            return View(poster);
        }

        //
        // GET: /PosterManager/Create

        public ActionResult Create()
        {
            ViewBag.BoardId = new SelectList(db.Boards, "BoardId", "Name");
            return View();
        } 

        //
        // POST: /PosterManager/Create

        [HttpPost]
        public ActionResult Create(Poster poster)
        {
            var image = WebImage.GetImageFromRequest();

            if (image != null)
            {
                if (image.Width > 500)
                {
                    image.Resize(500, ((500 * image.Height) / image.Width));
                }

                var filename = Path.GetFileName(image.FileName);
                image.Save(Path.Combine("../Uploads/Images", filename));
                filename = Path.Combine("~/Uploads/Images", filename);

                poster.PosterImgUrl = Url.Content(filename);

            }
            if (ModelState.IsValid)
            {
                poster.PublishDate = DateTime.Now;
                db.Posters.Add(poster);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ViewBag.BoardId = new SelectList(db.Boards, "BoardId", "Name",poster.BoardId);
            return View(poster);
        }
        
        //
        // GET: /PosterManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Poster poster = db.Posters.Find(id);
            ViewBag.BoardId = new SelectList(db.Boards, "BoardId", "Name",poster.BoardId);
            return View(poster);
        }

        //
        // POST: /PosterManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Poster poster)
        {
            if (ModelState.IsValid)
            {
                db.Entry(poster).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BoardId = new SelectList(db.Boards, "BoardId", "Name",poster.BoardId);
            return View(poster);
        }

        //
        // GET: /PosterManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            Poster poster = db.Posters.Find(id);
            return View(poster);
        }

        //
        // POST: /PosterManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Poster poster = db.Posters.Find(id);
            db.Posters.Remove(poster);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}